package com.woniuxy.caradmin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Insurerecord;
import com.woniuxy.caradmin.entity.pojo.Maintainrecord;
import com.woniuxy.caradmin.entity.pojo.Remind;
import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.entity.vo.RemindVo;
import com.woniuxy.caradmin.mapper.InsurerecordMapper;
import com.woniuxy.caradmin.mapper.MaintainrecordMapper;
import com.woniuxy.caradmin.mapper.RemindMapper;
import com.woniuxy.caradmin.mapper.YearcheckrecordMapper;
import com.woniuxy.caradmin.service.RemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class RemindServiceImpl implements RemindService {
    @Autowired
    private RemindMapper remindMapper;
    @Override
    public boolean selectExpiretime() {
        //年检到期查询并添加至提醒表
        List<Yearcheckrecord> yearcheckrecordList = remindMapper.selectYearcheckrecordExpietime();
        if (yearcheckrecordList != null && yearcheckrecordList.size() > 0) {
            for (Yearcheckrecord yearcheckrecord : yearcheckrecordList) {
                Remind remindYearcheck = new Remind();
                long longnow = System.currentTimeMillis();
                Date yearcheckNexttime = yearcheckrecord.getNexttime();
                long longyeartime = yearcheckNexttime.getTime();
                remindYearcheck.setVehiclenumid(yearcheckrecord.getVehiclefile().getId());
                remindYearcheck.setExpiretime(yearcheckNexttime);
                remindYearcheck.setTypee("年检");
                //查询年检记录中的车辆id、到期时间在提醒表中是否存在
                Remind remind = remindMapper.selectRemind(remindYearcheck);
                System.out.println("年检提醒是否存在-remind:"+remind);
                //已在提醒表当中
                if(remind != null){
                    if(longyeartime - longnow <= 0){
                        //更新提醒类别的信息
                        remindYearcheck.setTypee("年检已过期，请立即处理");
                        remindYearcheck.setId(remind.getId());
                        remindMapper.updateRemind(remindYearcheck);
                    } else if(longyeartime - longnow > 7 * 24 * 3600 * 1000L){
                        remindMapper.deleteRemind(remind);
                    }else{
                        remindYearcheck.setTypee("年检到期提醒");
                        remindYearcheck.setId(remind.getId());
                        remindMapper.updateRemind(remindYearcheck);
                    }
                }else{//不在提醒表当中
                    //如果年检到期时间-系统当前时间的差值小于0天
                    if(longyeartime - longnow <= 0){
                        remindYearcheck.setTypee("年检已过期，请立即处理");
                        remindMapper.addRemind(remindYearcheck);
                    }else if(longyeartime - longnow <= 7 * 24 * 3600 * 1000L){//如果年检到期时间-系统当前时间的差值小于7天
                        remindYearcheck.setTypee("年检到期提醒");
                        remindMapper.addRemind(remindYearcheck);
                    }
                }
            }
        }
        //保养到期查询并添加至提醒表
        List<Maintainrecord> maintainrecordList = remindMapper.selectMaintainrecordExpietime();
        if (maintainrecordList != null && maintainrecordList.size() > 0) {
            for (Maintainrecord maintainrecord : maintainrecordList) {
                Remind remindMaintain = new Remind();
                long longnow = System.currentTimeMillis();
                long maintainNexttime = maintainrecord.getNexttime().getTime();
                remindMaintain.setVehiclenumid(maintainrecord.getVehiclefile().getId());
                remindMaintain.setExpiretime(maintainrecord.getNexttime());
                remindMaintain.setTypee("保养");
                Remind remind = remindMapper.selectRemind(remindMaintain);
                System.out.println("保养提醒是否存在-remind:"+remind);
                if(remind!=null){
                    if(maintainNexttime - longnow <= 0){
                        remindMaintain.setTypee("保养已过期，请立即处理");
                        remindMaintain.setId(remind.getId());
                        remindMapper.updateRemind(remindMaintain);
                    }else if(maintainNexttime- longnow > 7 * 24 * 3600 * 1000L){
                        remindMapper.deleteRemind(remind);
                    }else{
                        remindMaintain.setTypee("保养到期提醒");
                        remindMaintain.setId(remind.getId());
                        remindMapper.updateRemind(remindMaintain);
                    }
                }else{
                    if(maintainNexttime - longnow <= 0){
                        remindMaintain.setTypee("保养已过期，请立即处理");
                        remindMapper.addRemind(remindMaintain);
                    }else if(maintainNexttime- longnow <= 7 * 24 * 3600 * 1000L){
                        remindMaintain.setTypee("保养到期提醒");
                        remindMapper.addRemind(remindMaintain);
                    }
                }
            }
        }
        //保险到期查询并添加至提醒表
        List<Insurerecord> insurerecordList = remindMapper.selectInsurerecordExpietime();
        if (insurerecordList != null && insurerecordList.size() > 0) {
            for (Insurerecord insurerecord : insurerecordList) {
                Remind remindInsure = new Remind();
                long longnow = System.currentTimeMillis();
                long insureEndtime = insurerecord.getEndtime().getTime();
                remindInsure.setVehiclenumid(insurerecord.getVehiclefile().getId());
                remindInsure.setExpiretime(insurerecord.getEndtime());
                remindInsure.setTypee("保险");
                Remind remind = remindMapper.selectRemind(remindInsure);
                System.out.println("保险提醒是否存在-remind:"+remind);
                if(remind!=null){
                    if(insureEndtime - longnow <=0){
                        remindInsure.setTypee("保险已过期，请立即处理");
                        remindInsure.setId(remind.getId());
                        remindMapper.updateRemind(remindInsure);
                    }else if(insureEndtime - longnow > 7 * 24 * 3600 * 1000L){
                        remindMapper.deleteRemind(remind);
                    }else{
                        remindInsure.setTypee("保险到期提醒");
                        remindInsure.setId(remind.getId());
                        remindMapper.updateRemind(remindInsure);
                    }
                }else{
                    if(insureEndtime - longnow <=0){
                        remindInsure.setTypee("保险已过期，请立即处理");
                        remindMapper.addRemind(remindInsure);
                    }else if(insureEndtime - longnow <= 7 * 24 * 3600 * 1000L){
                        remindInsure.setTypee("保险到期提醒");
                        remindMapper.addRemind(remindInsure);
                    }
                }
            }
        }
        return true;
    }

    @Override
    public List<Remind> selectAllRemind() {
        List<Remind> remindList = remindMapper.selectAllRemind();
        return remindList;
    }

    @Override
    public List<Remind> selectRemindByPage(RemindVo remindVo) {
        List<Remind> remindList = remindMapper.selectRemindByPage(remindVo);
        return remindList;
    }

    @Override
    public boolean deleteRemind(Remind remind) {
        remindMapper.deleteRemind(remind);
        return true;
    }
}
